Imports System
Imports System.IO
Imports System.Text
Imports System.Runtime.InteropServices
Public Class Form1
    Private Function Search_Themes(ByVal sThemeName As String)
        Dim i As Integer = 0
        For i = 0 To objSEApp.Customization.RibbonBarThemes.Count - 1
            If (objSEApp.Customization.RibbonBarThemes.Item(i).Name = sThemeName) Then
                Search_Themes = True
                Exit Function
            End If
        Next

        Search_Themes = False

    End Function
    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load




        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing
        Dim objNewRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing
        Dim objRibbonBar As SolidEdgeFramework.RibbonBar = Nothing
        Dim objNewTab As SolidEdgeFramework.RibbonBarTab = Nothing
        Dim objNewGroup As SolidEdgeFramework.RibbonBarGroup = Nothing
        Dim objNewControl As SolidEdgeFramework.RibbonBarControl = Nothing


        Try
            ' Get the type from the Solid Edge ProgID
            objSEType = Type.GetTypeFromProgID("SolidEdge.Application")
            ' Get the type from the Revision Manager ProgID
            objRevManType = Type.GetTypeFromProgID("RevisionManager.Application")

        Catch ex As Exception

        End Try



        Try
            ' Check to see if this macro has been added to Solid Edge as a Tab, Group, Ctrl.
            ' First connect to Solid Edge.
            If oConnectToSolidEdge(True, True) Then
                Dim bFound As Boolean = False

                ' Check to see if they have a custom theme.  If they don't then create one.
                If (objSEApp.Customization.RibbonBarThemes.Count > 0) Then
                    ' Check to see if this tab is on any ribbonbar on any of the themes.
                    bFound = Search_Themes("Gregs Theme")

                    ' If his theme is there then just continue 
                    If (bFound) Then
                        Exit Sub
                    End If
                End If

                ' If there isn't a theme then create it.
                If (bFound = False) Then
                    Try
 

                        ' Find out where this executable is located.
                        Dim aAsm As System.Reflection.Assembly = Nothing

                        Try
                            aAsm = System.Reflection.Assembly.GetExecutingAssembly()
                        Catch ex As Exception

                        End Try


                        ' Put this at the beginning  
                        objSEApp.Customization.BeginCustomization()

                        objNewRibbonBarTheme = objSEApp.Customization.RibbonBarThemes.Create(objRibbonBarTheme)
                        objNewRibbonBarTheme.Name = "Gregs Theme"



                        For i = 0 To objNewRibbonBarTheme.RibbonBars.Count - 1
                            If ("Detail" = objNewRibbonBarTheme.RibbonBars.Item(i).Environment) Then
                                objRibbonBar = objNewRibbonBarTheme.RibbonBars.Item(i)

                                ' Add a tab to the ribbon bar
                                objNewTab = objNewRibbonBarTheme.RibbonBars.Item(i).RibbonBarTabs.Insert("Label Dimensions", objNewRibbonBarTheme.RibbonBars.Item(i).RibbonBarTabs.Count, SolidEdgeFramework.RibbonBarInsertMode.seRibbonBarInsertCreate)
                                objNewTab.Visible = True

                                ' Add a group to the tab
                                objNewGroup = objNewTab.RibbonBarGroups.Insert("Dimensions", objNewRibbonBarTheme.RibbonBars.Item(i).RibbonBarTabs.Count, SolidEdgeFramework.RibbonBarInsertMode.seRibbonBarInsertCreate)
                                objNewGroup.Visible = True

                                Dim macroArray(1) As Object
                                macroArray(0) = aAsm.Location

                                objNewControl = objNewGroup.RibbonBarControls.Insert(macroArray, Nothing, SolidEdgeFramework.RibbonBarInsertMode.seRibbonBarInsertCreateButton)
                                objNewControl.Visible = True

                                Exit For
                            End If
                        Next

                        ' Set Greg's theme 
                        objSEApp.Customization.RibbonBarThemes.ActivateTheme(objNewRibbonBarTheme)
                        objSEApp.Customization.RibbonBarThemes.Commit()
                        objSEApp.Customization.EndCustomization()

                    Catch ex As Exception

                    End Try

                End If

            Else
                MessageBox.Show("Could not connect to or start Solid Edge!", "Label Dimensions", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End
            End If

        Catch ex As Exception

        End Try

        releaseObject(objRibbonBarTheme)
        releaseObject(objNewRibbonBarTheme)
        releaseObject(objRibbonBar)
        releaseObject(objNewTab)
        releaseObject(objNewGroup)
        releaseObject(objNewControl)
        ForceGarbageCollection()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        '.NET must do
        OleMessageFilter.Register()

        If oConnectToSolidEdge(True, True) Then
            If objSEApp.ActiveDocumentType = SolidEdgeFramework.DocumentTypeConstants.igDraftDocument Then
                Dim objSelectSet As SolidEdgeFramework.SelectSet = Nothing
                objSelectSet = objSEApp.ActiveDocument.selectset
                If objSelectSet.Count > 0 Then
                    Dim ii As Integer = 0
                    For ii = 1 To objSelectSet.Count
                        Dim objDimSelected As SolidEdgeFrameworkSupport.Dimension = Nothing
                        objDimSelected = objSelectSet.Item(ii)
                        If Me.RadioButtonCritical.Checked = True Then
                            AttachSymbolToDimension(objDimSelected, "Critical")
                        ElseIf Me.RadioButtonMajor.Checked = True Then
                            AttachSymbolToDimension(objDimSelected, "Major")
                        ElseIf Me.RadioButtonMinor.Checked = True Then
                            AttachSymbolToDimension(objDimSelected, "Minor")
                        End If
                    Next
                Else
                    MessageBox.Show("Please select the dimensions to mark as critical, major or minor!", "Label Dimensions", MessageBoxButtons.OK, _
                                    MessageBoxIcon.Exclamation)
                End If
            Else
                MessageBox.Show("You must run this macro from Solid Edge draft!", "Label Dimensions", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        Else
            MessageBox.Show("Could not connect to or start Solid Edge!", "Label Dimensions", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End
        End If

        '.NET must do
        OleMessageFilter.Revoke()
    End Sub

    Public Sub AttachSymbolToDimension(oDim As SolidEdgeFrameworkSupport.Dimension, strDimType As String)
        Dim objBalloon As SolidEdgeFrameworkSupport.Balloon = Nothing
        Dim objBalloons As SolidEdgeFrameworkSupport.Balloons = Nothing
        Dim objActiveDraftSheet As SolidEdgeDraft.Sheet = Nothing
        Dim oDimDisplayData As SolidEdgeFrameworkSupport.DisplayData = Nothing
        Dim OriginX, OriginY, OriginZ, X_DirX, X_DirY, X_DirZ, Z_DirX, Z_DirY, Z_DirZ As Double
        Dim oText As String = ""
        oDimDisplayData = oDim.GetDisplayData
        Try
            objActiveDraftSheet = objSEApp.ActiveDocument.activesheet
            objBalloons = objActiveDraftSheet.Balloons
            oDimDisplayData = oDim.GetDisplayData
            'index of zero is the dim text
            oDimDisplayData.GetTextAtIndex(0, oText, OriginX, OriginY, OriginZ, X_DirX, X_DirY, X_DirZ, Z_DirX, Z_DirY, Z_DirZ)
            Select Case strDimType
                Case "Critical"  'place a filled in diamond
                    objBalloon = objBalloons.AddByTerminator(oDim, OriginX - 0.002, OriginY + 0.002, 0, True)
                    objBalloon.Callout = True
                    objBalloon.Style.Font = "Arial"
                    objBalloon.BalloonText = ChrW(&H2666)
                Case "Major"  'place a filled in square
                    objBalloon = objBalloons.AddByTerminator(oDim, OriginX - 0.002, OriginY + 0.002, 0, True)
                    objBalloon.Callout = True
                    objBalloon.Style.Font = "Arial"
                    objBalloon.BalloonText = ChrW(&H25A0)  '"U+25A0"
                Case "Minor"  'place a filled in circle
                    objBalloon = objBalloons.AddByTerminator(oDim, OriginX - 0.002, OriginY + 0.002, 0, True)
                    objBalloon.Callout = True
                    objBalloon.Style.Font = "Arial"
                    objBalloon.BalloonText = ChrW(&H25CF)  ' "U+25CF"
            End Select
        Catch ex As Exception
            MessageBox.Show("Error placing callout to identify dimension.  the error is " + ex.Message, "Label Dimensions", _
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        'release stuff....  since a local variable when goes out of scope should be released but good practice to do it anyway!
        oRelease_Object(oDimDisplayData)
        oRelease_Object(objBalloon)
        oRelease_Object(objBalloons)
        oRelease_Object(objActiveDraftSheet)
        ForceGarbageCollection()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        End
    End Sub

    Private Sub Form1_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing



        releaseObject(objSEApp)
        ForceGarbageCollection()

    End Sub
End Class
